Ext.define('AM.controller.BaseController', {
	extend : 'Ext.app.Controller',
	views : [ 'base.List', 'base.Edit' ],
	fieldItems : [],
	init : function() {
		this.callParent(arguments);
	},
	refs : [ {
		ref : 'baseList',
		selector : 'basegridlist'
	} ],
	createEditView : function() {
		// 根据model的形式创建编辑窗口
		var view = Ext.widget('baseedit');
		// console.log(this.getStore(this.stores));
		view.storeName = this.stores;

		var formItem = [ {
			xtype : 'form',
			items : this.fieldItems
		} ];
		view.add(formItem);
		return view;
	},
	query : function(me) {
		console.log('BaseController query');
		var queryPanel = me.up('baselist').down('basequerypannel');
		var store = this.getStore(this.stores);

		store.remoteFilter = true;
		store.clearFilter(true);

		var filters = new Array();
		if (!queryPanel.getForm().isValid()) {
			return;
		}
		var values = queryPanel.getForm().getValues(false, true, true, true);
		Ext.Object.each(values, function(key, value, myself) {
			var filter = Ext.util.Filter.create({
				property : key,
				value : value
			});
			filters.push(filter);
		});

		if (filters.length > 0) {
			console.log(filters);
			store.filter(filters);
		}
	},
	edit : function(me) {
		console.log('BaseController edit');
		var baseList = me.up('baselist').down('basegridlist');
		var selectModel = baseList.getSelectionModel();
		if (!selectModel.hasSelection()) {
			console.log('no select');
			return;
		}

		var record = selectModel.getSelection()[0];
		var view = this.createEditView();
		view.down('form').loadRecord(record);
	},
	add : function() {
		console.log('BaseController add');
		// console.log(this.getStore(this.stores));
		var view = this.createEditView();

		console.log(view);
		var record = this.getModel(this.models).create();
		console.log(record);
		view.down('form').loadRecord(record);
	},
	del : function(me) {
		console.log('BaseController delRecord');
		var baseList = me.up('baselist').down('basegridlist');
		var selectModel = baseList.getSelectionModel();
		console.log(this);

		if (!selectModel.hasSelection()) {
			console.log('no select');
			return;
		}

		var record = selectModel.getSelection()[0];
		console.log(this.stores);
		var store = this.getStore(this.stores);
		store.remove(record);
		store.sync();
	}
});